<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>类与对象和包 | 浪飞IT小栈</title>
    <meta name="generator" content="VuePress 1.9.9">
    <link rel="icon" href="/img/favicon.ico">
    <meta name="description" content="前后端技术分享">
    <meta name="keywords" content="前端博客,后端博客,人工智能,个人技术博客,前端,前端开发,前端框架,web前端,前端面试题,技术文档,学习,面试,JavaScript,js,ES6,TypeScript,vue,python,css3,html5,Node,git,github,markdown,java,Spring,Springboot,Redis,mysql">
    <meta name="baidu-site-verification" content="7F55weZDDc">
    <meta name="theme-color" content="#11a8cd">
    
    <link rel="preload" href="/assets/css/0.styles.ada71c49.css" as="style"><link rel="preload" href="/assets/js/app.a2a00aa5.js" as="script"><link rel="preload" href="/assets/js/2.256f807a.js" as="script"><link rel="preload" href="/assets/js/18.4536fd5c.js" as="script"><link rel="prefetch" href="/assets/js/10.ab144fe3.js"><link rel="prefetch" href="/assets/js/11.062e0879.js"><link rel="prefetch" href="/assets/js/12.f9c60882.js"><link rel="prefetch" href="/assets/js/13.0a6ebfcf.js"><link rel="prefetch" href="/assets/js/14.03fc6f12.js"><link rel="prefetch" href="/assets/js/15.19dbf9fd.js"><link rel="prefetch" href="/assets/js/16.6e68160b.js"><link rel="prefetch" href="/assets/js/17.1d678834.js"><link rel="prefetch" href="/assets/js/19.9c1b65df.js"><link rel="prefetch" href="/assets/js/20.c5f96e71.js"><link rel="prefetch" href="/assets/js/21.3f0b334e.js"><link rel="prefetch" href="/assets/js/22.55c868be.js"><link rel="prefetch" href="/assets/js/23.91286241.js"><link rel="prefetch" href="/assets/js/24.c1af1909.js"><link rel="prefetch" href="/assets/js/25.a995a755.js"><link rel="prefetch" href="/assets/js/26.abc5dea3.js"><link rel="prefetch" href="/assets/js/27.1eb62850.js"><link rel="prefetch" href="/assets/js/28.b383a871.js"><link rel="prefetch" href="/assets/js/29.088e0d73.js"><link rel="prefetch" href="/assets/js/3.cd716564.js"><link rel="prefetch" href="/assets/js/30.3ccc98d5.js"><link rel="prefetch" href="/assets/js/31.aab7cc23.js"><link rel="prefetch" href="/assets/js/32.5e16b7e6.js"><link rel="prefetch" href="/assets/js/33.4aaeb741.js"><link rel="prefetch" href="/assets/js/34.45e8e898.js"><link rel="prefetch" href="/assets/js/35.5899c97a.js"><link rel="prefetch" href="/assets/js/36.32e2e263.js"><link rel="prefetch" href="/assets/js/37.c4e27bbd.js"><link rel="prefetch" href="/assets/js/38.fe5e60d2.js"><link rel="prefetch" href="/assets/js/39.2cb0b4ee.js"><link rel="prefetch" href="/assets/js/4.edf4d5e7.js"><link rel="prefetch" href="/assets/js/40.08d5bead.js"><link rel="prefetch" href="/assets/js/41.2f80f24b.js"><link rel="prefetch" href="/assets/js/42.43a49913.js"><link rel="prefetch" href="/assets/js/43.08da7ec2.js"><link rel="prefetch" href="/assets/js/44.7c7cb8a9.js"><link rel="prefetch" href="/assets/js/45.0ddf7e7f.js"><link rel="prefetch" href="/assets/js/46.7c4609a5.js"><link rel="prefetch" href="/assets/js/47.edf3ddf8.js"><link rel="prefetch" href="/assets/js/48.569a90b9.js"><link rel="prefetch" href="/assets/js/49.fa368ac8.js"><link rel="prefetch" href="/assets/js/5.24054156.js"><link rel="prefetch" href="/assets/js/50.b9b3ea7f.js"><link rel="prefetch" href="/assets/js/51.a2ebf0fe.js"><link rel="prefetch" href="/assets/js/52.31f02238.js"><link rel="prefetch" href="/assets/js/53.4110ef1e.js"><link rel="prefetch" href="/assets/js/54.dab741c7.js"><link rel="prefetch" href="/assets/js/55.f27984e9.js"><link rel="prefetch" href="/assets/js/56.bd4c1a8e.js"><link rel="prefetch" href="/assets/js/57.375d40cb.js"><link rel="prefetch" href="/assets/js/58.932e3864.js"><link rel="prefetch" href="/assets/js/59.e657be1b.js"><link rel="prefetch" href="/assets/js/6.b259c061.js"><link rel="prefetch" href="/assets/js/60.88d94274.js"><link rel="prefetch" href="/assets/js/61.fa71d884.js"><link rel="prefetch" href="/assets/js/62.a9d3b3b7.js"><link rel="prefetch" href="/assets/js/63.f849fe75.js"><link rel="prefetch" href="/assets/js/64.1770f7a9.js"><link rel="prefetch" href="/assets/js/65.6afc33db.js"><link rel="prefetch" href="/assets/js/66.799f2eb4.js"><link rel="prefetch" href="/assets/js/67.f36b2f7d.js"><link rel="prefetch" href="/assets/js/68.868cd00b.js"><link rel="prefetch" href="/assets/js/69.4c29b907.js"><link rel="prefetch" href="/assets/js/7.e34ea00f.js"><link rel="prefetch" href="/assets/js/70.1b993ed4.js"><link rel="prefetch" href="/assets/js/71.e16ad4f9.js"><link rel="prefetch" href="/assets/js/72.627505e6.js"><link rel="prefetch" href="/assets/js/73.32768e14.js"><link rel="prefetch" href="/assets/js/74.968a580d.js"><link rel="prefetch" href="/assets/js/75.42d5af3d.js"><link rel="prefetch" href="/assets/js/76.19a69d31.js"><link rel="prefetch" href="/assets/js/77.cf627a78.js"><link rel="prefetch" href="/assets/js/78.07177880.js"><link rel="prefetch" href="/assets/js/8.c4dd00ce.js"><link rel="prefetch" href="/assets/js/9.0ff73be2.js">
    <link rel="stylesheet" href="/assets/css/0.styles.ada71c49.css">
  </head>
  <body class="theme-mode-light">
    <div id="app" data-server-rendered="true"><div class="theme-container sidebar-open have-rightmenu"><header class="navbar blur"><div title="目录" class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/img/logo.png" alt="浪飞IT小栈" class="logo"> <span class="site-name can-hide">浪飞IT小栈</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><a href="/pages/6138ae/" class="nav-link">Java速通</a></div><div class="nav-item"><a href="/pages/705b35/" class="nav-link">面试经典</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="精品项目" class="dropdown-title"><a href="/project/" class="link-title">精品项目</a> <span class="title" style="display:none;">精品项目</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>桌面端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">JavaScript</a></li></ul></li><li class="dropdown-item"><h4>PC后端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/note/javascript/" class="nav-link">项目1</a></li><li class="dropdown-subitem"><a href="/note/typescript-axios/" class="nav-link">项目二</a></li></ul></li></ul></div></div><div class="nav-item"><a href="/about/" class="nav-link">电子书</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/archives/" class="nav-link">归档</a></li></ul></div></div><div class="nav-item"><a href="/pages/0c07b2/" class="nav-link">IT杂货铺</a></div><div class="nav-item"><a href="/archives/" class="nav-link">更多资源</a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar-hover-trigger"></div> <aside class="sidebar" style="display:none;"><div class="blogger"><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/avatar.jpg"> <div class="blogger-info"><h3>浪飞yes</h3> <span>无法简单的人儿~</span></div></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><a href="/pages/6138ae/" class="nav-link">Java速通</a></div><div class="nav-item"><a href="/pages/705b35/" class="nav-link">面试经典</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="精品项目" class="dropdown-title"><a href="/project/" class="link-title">精品项目</a> <span class="title" style="display:none;">精品项目</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>桌面端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">JavaScript</a></li></ul></li><li class="dropdown-item"><h4>PC后端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/note/javascript/" class="nav-link">项目1</a></li><li class="dropdown-subitem"><a href="/note/typescript-axios/" class="nav-link">项目二</a></li></ul></li></ul></div></div><div class="nav-item"><a href="/about/" class="nav-link">电子书</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/archives/" class="nav-link">归档</a></li></ul></div></div><div class="nav-item"><a href="/pages/0c07b2/" class="nav-link">IT杂货铺</a></div><div class="nav-item"><a href="/archives/" class="nav-link">更多资源</a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>开篇</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>基础语法</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>面向对象</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/pages/294c58/" aria-current="page" class="active sidebar-link">类与对象和包</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level2"><a href="/pages/294c58/#一、今日学习内容与目标" class="sidebar-link">一、今日学习内容与目标</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/294c58/#_1-1-今日学习内容" class="sidebar-link">1.1 今日学习内容</a></li><li class="sidebar-sub-header level3"><a href="/pages/294c58/#_1-2-今日学习目标" class="sidebar-link">1.2 今日学习目标</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/294c58/#二、面向对象基础" class="sidebar-link">二、面向对象基础</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/294c58/#_2-1-面向对象思想-了解" class="sidebar-link">2.1 面向对象思想（了解）</a></li><li class="sidebar-sub-header level3"><a href="/pages/294c58/#_2-2-认识类和对象-知道" class="sidebar-link">2.2 认识类和对象 （知道）</a></li><li class="sidebar-sub-header level3"><a href="/pages/294c58/#_2-3-类的定义-重点掌握" class="sidebar-link">2.3 类的定义（重点掌握）</a></li><li class="sidebar-sub-header level3"><a href="/pages/294c58/#_2-4-创建对象和使用对象-重点掌握" class="sidebar-link">2.4 创建对象和使用对象（重点掌握）</a></li><li class="sidebar-sub-header level4"><a href="/pages/294c58/#_2-4-1-对象基本操作-重点掌握" class="sidebar-link">2.4.1. 对象基本操作（重点掌握）</a></li><li class="sidebar-sub-header level4"><a href="/pages/294c58/#_2-4-2-对象创建过程-内存分析-理解即可" class="sidebar-link">2.4.2. 对象创建过程-内存分析（理解即可）</a></li><li class="sidebar-sub-header level4"><a href="/pages/294c58/#_2-4-3-类和对象的关系-了解" class="sidebar-link">2.4.3 类和对象的关系（了解）</a></li><li class="sidebar-sub-header level3"><a href="/pages/294c58/#_2-5-构造方法-重点掌握" class="sidebar-link">2.5 构造方法（重点掌握）</a></li><li class="sidebar-sub-header level3"><a href="/pages/294c58/#_2-6-局部变量和成员变量区别-理解记忆" class="sidebar-link">2.6 局部变量和成员变量区别 ( 理解记忆 )</a></li><li class="sidebar-sub-header level3"><a href="/pages/294c58/#_2-7-this-关键字" class="sidebar-link">2.7 this 关键字</a></li><li class="sidebar-sub-header level4"><a href="/pages/294c58/#_2-7-1-this关键字-掌握" class="sidebar-link">2.7.1 this关键字（掌握）</a></li><li class="sidebar-sub-header level4"><a href="/pages/294c58/#_2-7-2-使用构造器还是setter方法-了解" class="sidebar-link">2.7.2 使用构造器还是setter方法（了解）</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/294c58/#三、封装思想" class="sidebar-link">三、封装思想</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/294c58/#_3-1-封装思想概述-理解" class="sidebar-link">3.1 封装思想概述 （理解）</a></li><li class="sidebar-sub-header level3"><a href="/pages/294c58/#_3-2-访问修饰符-必须记住" class="sidebar-link">3.2 访问修饰符（必须记住）</a></li><li class="sidebar-sub-header level3"><a href="/pages/294c58/#_3-3-getter方法和setter方法" class="sidebar-link">3.3 getter方法和setter方法</a></li><li class="sidebar-sub-header level3"><a href="/pages/294c58/#_3-4-javabean规范-重点掌握" class="sidebar-link">3.4 JavaBean规范（重点掌握）</a></li><li class="sidebar-sub-header level3"><a href="/pages/294c58/#_3-5-包管理" class="sidebar-link">3.5 包管理</a></li><li class="sidebar-sub-header level4"><a href="/pages/294c58/#_3-5-1-包定义-package-会用" class="sidebar-link">3.5.1 包定义-package（会用）</a></li><li class="sidebar-sub-header level4"><a href="/pages/294c58/#_3-5-2-导包-import-掌握" class="sidebar-link">3.5.2 导包-import（掌握）</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/294c58/#四、小结与作业" class="sidebar-link">四、小结与作业</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/294c58/#_4-1-小结" class="sidebar-link">4.1 小结</a></li><li class="sidebar-sub-header level3"><a href="/pages/294c58/#_4-2-作业" class="sidebar-link">4.2 作业</a></li><li class="sidebar-sub-header level4"><a href="/pages/294c58/#_4-2-1-题目1" class="sidebar-link">4.2.1 题目1</a></li><li class="sidebar-sub-header level4"><a href="/pages/294c58/#_4-2-2-题目2" class="sidebar-link">4.2.2 题目2</a></li><li class="sidebar-sub-header level4"><a href="/pages/294c58/#_4-2-3-题目3" class="sidebar-link">4.2.3 题目3</a></li><li class="sidebar-sub-header level4"><a href="/pages/294c58/#_4-2-4-题目4" class="sidebar-link">4.2.4 题目4</a></li></ul></li></ul></li><li><a href="/pages/6db17e/" class="sidebar-link">继承与抽象</a></li><li><a href="/pages/637d05/" class="sidebar-link">接口与多态</a></li><li><a href="/pages/efafe7/" class="sidebar-link">查漏补缺</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>常用类</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>基础高级</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>数据库</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>前端技术</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>热门框架</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>前后端分离</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>权限控制</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>NoSQL</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>微服务</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>消息中间件</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>脚手架</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>技术增值</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <div><main class="page"><div class="theme-vdoing-wrapper "><div class="articleInfo-wrap" data-v-06225672><div class="articleInfo" data-v-06225672><ul class="breadcrumbs" data-v-06225672><li data-v-06225672><a href="/" title="首页" class="iconfont icon-home router-link-active" data-v-06225672></a></li> <li data-v-06225672><a href="/categories/?category=Java%E9%80%9F%E9%80%9A" title="分类" data-v-06225672>Java速通</a></li><li data-v-06225672><a href="/categories/?category=%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1" title="分类" data-v-06225672>面向对象</a></li></ul> <div class="info" data-v-06225672><div title="作者" class="author iconfont icon-touxiang" data-v-06225672><a href="https://github.com/langfeiyes" target="_blank" title="作者" class="beLink" data-v-06225672>langfeiyes</a></div> <div title="创建时间" class="date iconfont icon-riqi" data-v-06225672><a href="javascript:;" data-v-06225672>2024-02-28</a></div> <!----></div></div></div> <!----> <div class="content-wrapper"><div class="right-menu-wrapper"><div class="right-menu-margin"><div class="right-menu-title">目录</div> <div class="right-menu-content"></div></div></div> <h1><img src="">类与对象和包<!----></h1>  <div class="theme-vdoing-content content__default"><h1 id="类与对象和包"><a href="#类与对象和包" class="header-anchor">#</a> 类与对象和包</h1> <h2 id="一、今日学习内容与目标"><a href="#一、今日学习内容与目标" class="header-anchor">#</a> 一、今日学习内容与目标</h2> <h3 id="_1-1-今日学习内容"><a href="#_1-1-今日学习内容" class="header-anchor">#</a> 1.1 今日学习内容</h3> <ul><li><p>面向对象基础</p> <ul><li>面向对象思维</li> <li>类的定义</li> <li>对象的创建和对象的操作</li> <li>构造方法</li></ul></li> <li><p>封装思想</p> <ul><li>访问权限修饰符</li> <li>JavaBean规范</li> <li>this关键字</li> <li>包</li></ul></li></ul> <h3 id="_1-2-今日学习目标"><a href="#_1-2-今日学习目标" class="header-anchor">#</a> 1.2 今日学习目标</h3> <ul><li>了解什么是面向对象思维</li> <li><strong>必须掌握类的定义</strong></li> <li><strong>必须掌握对象的创建和使用对象</strong></li> <li><strong>必须掌握构造器的定义和作用</strong></li> <li>理解封装思想的好处</li> <li><strong>必须掌握private和public两个访问权限修饰符</strong></li> <li><strong>必须掌握JavaBean规范的四个要求</strong></li> <li>掌握使用this解决二义性</li> <li>了解如何选用构造器和setter方法设置对象内容</li> <li>了解import和package关键字</li></ul> <h2 id="二、面向对象基础"><a href="#二、面向对象基础" class="header-anchor">#</a> 二、面向对象基础</h2> <h3 id="_2-1-面向对象思想-了解"><a href="#_2-1-面向对象思想-了解" class="header-anchor">#</a> 2.1 面向对象思想（了解）</h3> <p>面向对象并不是一个技术，而是一种编程的指导思想，这个指导思想的关键点在于，我们应该</p> <ul><li>以什么形式 组织代码</li> <li>以什么思路 解决问题</li></ul> <p>为什么要学习面向对象?
因为生活中，我们解决问题时，就是采用这种指导思想去解决的
所以，我们写程序去解决问题时，如果也能采用这种指导思想就会使编程变得非常简单，程序也便于人理解</p> <p>举个例子，小明完成吃饭、写代码</p> <p><em><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/XmBI4lKUvsbR2ho.jpg" alt=""></em></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Test</span> <span class="token punctuation">{</span>
   <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
       <span class="token comment">// 需求: 小明完成吃饭、写代码</span>
       <span class="token comment">// 使用面向过程思想</span>
       <span class="token comment">/** 小明.买菜
        * 小明.炒菜   
        * ...
        */</span>
       
       <span class="token comment">// 使用面向对象思想</span>
       <span class="token comment">/** 菲佣.做饭 
        * 小明.吃饭
        * ...
        * 程序员.coding
        */</span>
   <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token comment">/**
* 面向过程：是一种以过程为中心的编程思想，实现功能的每一步，都是我自己实现的
* 面向对象：是一种以对象为中心的编程思想，通过指挥对象实现具体的功能
*/</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div><p>面向对象思想：<strong>强调我该让谁( 对象 )来做</strong></p> <p>使用面向对象思想发现：</p> <ul><li>使用面向对象思想可以将复杂的问题简单化</li> <li>将我们从执行者的位置，变成了指挥者</li> <li>客观存在的任何一种事物，都可以看做为程序中的对象（我们身边的任何事物都是对象）</li></ul> <p>面向对象更符合我们常规的思维方式，稳定性好，可重用性强，易于开发大型软件产品，有良好的可维护性，它拥有<strong>三大特征</strong>：</p> <ul><li>封装</li> <li>继承</li> <li>多态</li></ul> <p>Java中要创建对象，必须先要有类的存在</p> <h3 id="_2-2-认识类和对象-知道"><a href="#_2-2-认识类和对象-知道" class="header-anchor">#</a> 2.2 认识类和对象 （知道）</h3> <p>​    <strong>类：</strong> 指的是一组特征和行为( 功能 )的集合，可以将类理解为一张设计图</p> <p>**对象：**根据设计图，创造出来的实体</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/lzoQV82bJ3keZyx.png" style="zoom:67%;">  - - - → <img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/xGPwTUcEqgS2LCB.png" style="zoom:67%;"></p> <p>类和对象的关系</p> <ul><li>依存关系：先有类，才能创建对象</li> <li>数量关系：根据类，可以创建很多个对象</li></ul> <h3 id="_2-3-类的定义-重点掌握"><a href="#_2-3-类的定义-重点掌握" class="header-anchor">#</a> 2.3 类的定义（重点掌握）</h3> <p>类的组成：特征和行为</p> <p>定义类，就从特征和行为上分析，那么怎么来表示特征和行为呢？</p> <ul><li><p>特征：在类中通过<strong>成员变量</strong>来体现（类中方法外的变量）</p></li> <li><p>行为：在类中通过<strong>成员方法</strong>来体现（和前面的方法相比去掉static关键字即可）</p></li></ul> <p>语法格式：</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> 类名 <span class="token punctuation">{</span>
    
	<span class="token comment">// 编写成员变量(可以多个)</span>
	<span class="token punctuation">[</span>修饰符<span class="token punctuation">]</span> 数据类型	变量名<span class="token number">1</span><span class="token punctuation">;</span>
	<span class="token punctuation">[</span>修饰符<span class="token punctuation">]</span> 数据类型	变量名<span class="token number">2</span><span class="token punctuation">;</span>

	<span class="token comment">// 编写成员方法(可以多个)</span>
	<span class="token punctuation">[</span>修饰符<span class="token punctuation">]</span> 返回值类型 方法名称<span class="token punctuation">(</span>参数列表<span class="token punctuation">)</span><span class="token punctuation">{</span>
        
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>

例如<span class="token operator">:</span> 
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Person</span> <span class="token punctuation">{</span>
    <span class="token class-name">String</span> name<span class="token punctuation">;</span>
    <span class="token keyword">int</span> age<span class="token punctuation">;</span>
    
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">sayHi</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;大家好&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div><p>按照类定义的格式，定义出来一个类是完全没有问题的，但问题是：</p> <ul><li>我不知道什么属于特征，该用成员变量定义</li> <li>我不知道什么属于行为，该用成员方法定义</li></ul> <p>类的本质：对事物 ( 特征和行为 ) 的描述</p> <p>案例1 : 我的同桌，叫做张二狗，今年17岁了，178的身高，平时喜欢学java和干饭
案例2 : 刚买了一个iPhone14，花了8999块，老疼了，但是打电话和电话视频功能很nice
案例3 : 之前有个前女友，叫阿珍 ,60多岁了，会Java</p> <p>总结发现：特征就看事物有什么；行为( 功能 )就看事物能干什么？</p> <p>需求：定义一个学生类Student，学生有名字和年龄两个特征，有自我介绍( 控制台打印名字和年龄 )的行为。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">/** 
 * 步骤:
 * step1: 定义类，取类名(建议大写驼峰命名)
 * step2: 定义成员变量
 * step3: 定义成员方法
 */</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Student</span> <span class="token punctuation">{</span>
    <span class="token class-name">String</span> name<span class="token punctuation">;</span>
    <span class="token keyword">int</span> age<span class="token punctuation">;</span>

    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">sayHi</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;我是&quot;</span> <span class="token operator">+</span> name <span class="token operator">+</span> <span class="token string">&quot;,我今年&quot;</span> <span class="token operator">+</span> age <span class="token operator">+</span> <span class="token string">&quot;岁&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><p>到此，我们知道了类是一组特征和行为的集合，类可以理解为一张设计图，在实际中，我们会拿一张设计图打电话吗？</p> <p>我们生活中使用的是根据设计图创造出来的实体 ( 具体的手机 ) ，所以，我们需要通过类创造对象</p> <p>为此，我们的工程中组织代码的方式，也发生了变化：</p> <ul><li>使用面向对象思维的编写程序时，描述事物的类(Student)和测试类(xxDemo)分开来编写</li> <li>描述事物的类，不需要定义main方法，专门在测试类中提供main方法</li></ul> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/smhjaGAp8v3zwig.jpg" alt=""></strong></p> <h3 id="_2-4-创建对象和使用对象-重点掌握"><a href="#_2-4-创建对象和使用对象-重点掌握" class="header-anchor">#</a> 2.4 创建对象和使用对象（重点掌握）</h3> <p>对象是类的一个具体<strong>实例</strong>（实实在在的例子），它用于强调一个具体的个体。</p> <h4 id="_2-4-1-对象基本操作-重点掌握"><a href="#_2-4-1-对象基本操作-重点掌握" class="header-anchor">#</a> 2.4.1. 对象基本操作（重点掌握）</h4> <ul><li>创建对象 （也叫初始化对象）</li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code>类名 对象名 <span class="token operator">=</span> <span class="token keyword">new</span>  类名<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

例如<span class="token operator">:</span>
<span class="token class-name">Student</span> stu <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

直接打印对象的时候，打印的是类似于数组一样的内存地址<span class="token punctuation">,</span>格式如：类名<span class="token annotation punctuation">@3294e4f4</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>对象名<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><ul><li>使用对象</li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span> 给成员变量设置数据
对象名<span class="token punctuation">.</span>成员变量 <span class="token operator">=</span> 值<span class="token punctuation">;</span>

<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span> 获取成员变量的数据
数据类型 变量 <span class="token operator">=</span> 对象名<span class="token punctuation">.</span>字段名<span class="token punctuation">;</span>

<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span> 对象调用方法
对象名<span class="token punctuation">.</span>方法<span class="token punctuation">(</span>参数<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>测试代码如下：</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">StudentDemo</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 1 创建Student对象,对象的变量名为stu</span>
        <span class="token class-name">Student</span> stu <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">// 2 获取字段的值,先看看字段的初始值</span>
        <span class="token class-name">String</span> name <span class="token operator">=</span> stu<span class="token punctuation">.</span>name<span class="token punctuation">;</span>
        <span class="token keyword">int</span> age <span class="token operator">=</span> stu<span class="token punctuation">.</span>age<span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;名字=&quot;</span> <span class="token operator">+</span> name<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;年龄=&quot;</span> <span class="token operator">+</span> age<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">// 3 给字段设置值</span>
        stu<span class="token punctuation">.</span>name <span class="token operator">=</span> <span class="token string">&quot;汤姆&quot;</span><span class="token punctuation">;</span>
        stu<span class="token punctuation">.</span>age <span class="token operator">=</span> <span class="token number">5</span><span class="token punctuation">;</span>

        <span class="token comment">// 4 设置字段值后，再次获取字段的值</span>
        name <span class="token operator">=</span> stu<span class="token punctuation">.</span>name<span class="token punctuation">;</span>
        age <span class="token operator">=</span> stu<span class="token punctuation">.</span>age<span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;名字=&quot;</span> <span class="token operator">+</span> name<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;年龄=&quot;</span> <span class="token operator">+</span> age<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">// 5 调用对象的方法</span>
        stu<span class="token punctuation">.</span><span class="token function">sayHi</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 说话的功能</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token comment">/**
 * 总结
 * 1 类是一种新的数据类型(自定义的)，可以定义变量(面向对象的世界称为对象)
 * 2 通过类创建出来的对象，一定具有该类的特征和行为，属于对象的成员变量和成员方法
 * 所以，使用对象时，一定要用对象名来操作
*/</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br></div></div><h4 id="_2-4-2-对象创建过程-内存分析-理解即可"><a href="#_2-4-2-对象创建过程-内存分析-理解即可" class="header-anchor">#</a> 2.4.2. 对象创建过程-内存分析（理解即可）</h4> <p>分析内存的目的，是为了更清楚数据在内存中的存储和变化，方便我们理解和分析</p> <ul><li>创建对象 Student stu = new Student();</li></ul> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/d9Fh1E2INHGrsou.png" alt="">**</p> <p>此时通过stu对象查看name和age，初始值分别是null和0。</p> <ul><li>通过对象给字段设置数据	stu.name = “汤姆”;	stu.age = 17;</li></ul> <p><em><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/CDVP1BX4EjFyzbs.png" alt=""></em></p> <p>此时通过stu对象查看name和age的值，分别是汤姆 和17。</p> <ul><li>通过对象调用方法	stu.sayHi();  System.out.println(&quot;我是&quot; + name + &quot;,我今年&quot; + age + &quot;岁&quot;);</li></ul> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/HU3Db2yehIXukiQ.png" alt=""></strong></p> <ul><li>sayHi方法调用结束，也就是stu.sayHi();代码执行完成。sayHi()方法栈帧从栈内存销毁（出栈）。</li></ul> <h4 id="_2-4-3-类和对象的关系-了解"><a href="#_2-4-3-类和对象的关系-了解" class="header-anchor">#</a> 2.4.3 类和对象的关系（了解）</h4> <p>面向对象思想中有两个非常重要的概念，类和对象，其中:</p> <ul><li><p>类（class），是对某一类事物的描述（特征和行为），一般是对群体的描述，如下图的抽象女人图。</p></li> <li><p>对象（object），表示现实生活中该类事物的个体，也称之为实例，如下图的每一个具体的女人。</p></li> <li><p>类可以看作是对象的<strong>数据类型</strong>，就好比无论你是谁，只要是女人，那么类型就是女人。</p></li></ul> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/vOyKnQ5kFfijxcY.jpg" alt=""></p> <p>那么，类和对象的关系</p> <ul><li><strong>Java中需要根据类，创建对象</strong></li> <li><strong>一个类，可以创建出多个对象</strong></li></ul> <h3 id="_2-5-构造方法-重点掌握"><a href="#_2-5-构造方法-重点掌握" class="header-anchor">#</a> 2.5 构造方法（重点掌握）</h3> <p>构造方法，也称之为构造器（<strong>Constructor</strong>）。<strong><span style="color:blue;font-size:1.2em;">构</span>建、创<span style="color:blue;font-size:1.2em;">造</span>对象的时候，所调用的方法</strong>。</p> <p>构造方法的语法和特点：</p> <ul><li>方法名与类名相同，大小写也需要一致</li> <li>没有返回值类型，连void都没有</li> <li>没有具体的返回值 ( 不能使用 <code>return xx;</code> 语句 )</li></ul> <p>案例：为Student类提供一个构造方法</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Student</span> <span class="token punctuation">{</span>
    
    <span class="token keyword">public</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;我是Student构造方法&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>Student测试类</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">StudentDemo</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 使用 new + 构造器 来创建对象</span>
        <span class="token class-name">Student</span> stu <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Student</span> stu2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>通过测试发现，构造方法的执行时机  :</p> <ul><li>在创建对象的时候，被调用执行</li> <li>每创建一次对象，就会执行一次构造方法</li></ul> <p>接下来，我们来看看构造方法的作用</p> <ul><li>本质的作用 : 创建对象</li> <li>结合执行时机 :可以创建对象的时候，给对象中的数据初始化</li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">// Student类</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Student</span> <span class="token punctuation">{</span>
    <span class="token class-name">String</span> name<span class="token punctuation">;</span>
    <span class="token keyword">int</span> age<span class="token punctuation">;</span>

    <span class="token keyword">public</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        name <span class="token operator">=</span> <span class="token string">&quot;will&quot;</span><span class="token punctuation">;</span>
        age <span class="token operator">=</span> <span class="token number">17</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token comment">// 测试类</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">StudentDemo2</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">Student</span> stu1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>stu1<span class="token punctuation">.</span>name <span class="token operator">+</span> <span class="token string">&quot;-&quot;</span> <span class="token operator">+</span> stu1<span class="token punctuation">.</span>age<span class="token punctuation">)</span><span class="token punctuation">;</span>
        
        <span class="token class-name">Student</span> stu2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>stu2<span class="token punctuation">.</span>name <span class="token operator">+</span> <span class="token string">&quot;-&quot;</span> <span class="token operator">+</span> stu2<span class="token punctuation">.</span>age<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div><p>当然，构造方法也可以传入参数，给成员变量设置初始值</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">// Student类</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Student</span> <span class="token punctuation">{</span>
    <span class="token class-name">String</span> name<span class="token punctuation">;</span>
    <span class="token keyword">int</span> age<span class="token punctuation">;</span>

    <span class="token keyword">public</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token class-name">String</span> aName<span class="token punctuation">,</span><span class="token keyword">int</span> aAge<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        name <span class="token operator">=</span> aNme<span class="token punctuation">;</span>
        age <span class="token operator">=</span> aAge<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token comment">// 测试类</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">StudentDemo2</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">Student</span> stu1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>stu1<span class="token punctuation">.</span>name <span class="token operator">+</span> <span class="token string">&quot;-&quot;</span> <span class="token operator">+</span> stu1<span class="token punctuation">.</span>age<span class="token punctuation">)</span><span class="token punctuation">;</span>
        
        <span class="token class-name">Student</span> stu2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>stu2<span class="token punctuation">.</span>name <span class="token operator">+</span> <span class="token string">&quot;-&quot;</span> <span class="token operator">+</span> stu2<span class="token punctuation">.</span>age<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div><p>注意事项：</p> <ul><li>一个类中，没有写构造方法，编译器自动加一个默认的，不带参数的构造方法</li> <li>一个类中，如果写了构造方法，系统将不会提供一个默认的无参数的构造方法</li></ul> <p>好的编程习惯（建议）：</p> <p><strong>编写类的时候，无参构造，带参构造，全部手动给出！！！</strong></p> <h3 id="_2-6-局部变量和成员变量区别-理解记忆"><a href="#_2-6-局部变量和成员变量区别-理解记忆" class="header-anchor">#</a> 2.6 局部变量和成员变量区别 ( 理解记忆 )</h3> <table><thead><tr><th>区          别</th> <th>局部变量</th> <th>成员变量</th></tr></thead> <tbody><tr><td>定义的位置</td> <td>在方法的内部</td> <td>在方法的外部，直接写在类中</td></tr> <tr><td>使用范围</td> <td>只有方法当中才可以使用，出了方法就不能再用了</td> <td>整个类都可以通用</td></tr> <tr><td>默认值</td> <td>没有默认值，如果要想使用，必须手动进行赋值</td> <td>如果没有赋值，会有默认值,规则和数组一样</td></tr> <tr><td>内存的位置</td> <td>位于栈内存。<br>随着方法调用而诞生，随着方法调用完成而消失</td> <td>位于堆内存。<br>随着对象的创建而诞生，随着对象被回收而消失</td></tr></tbody></table> <p>注意：变量的使用范围，<strong>主要看变量所在的那对花括号{}</strong></p> <p>练习：区分局部变量和成员变量</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Student</span> <span class="token punctuation">{</span>
    <span class="token class-name">String</span> name<span class="token punctuation">;</span>
    <span class="token keyword">int</span> age<span class="token punctuation">;</span>
    
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">doWork</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
		<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> arr<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
			<span class="token keyword">int</span> ele <span class="token operator">=</span> arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>  
			<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>ele<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">sayHi</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;我是&quot;</span> <span class="token operator">+</span> name <span class="token operator">+</span> <span class="token string">&quot;,我今年&quot;</span> <span class="token operator">+</span> age <span class="token operator">+</span> <span class="token string">&quot;岁&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><h3 id="_2-7-this-关键字"><a href="#_2-7-this-关键字" class="header-anchor">#</a> 2.7 this 关键字</h3> <h4 id="_2-7-1-this关键字-掌握"><a href="#_2-7-1-this关键字-掌握" class="header-anchor">#</a> 2.7.1 this关键字（掌握）</h4> <p>之前说过，变量名称或方法参数名称，要见名知意，下列两个set方法的参数名，就显得太LOW了。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Student</span> <span class="token punctuation">{</span>
	<span class="token keyword">private</span> <span class="token class-name">String</span> name<span class="token punctuation">;</span>
	<span class="token keyword">private</span> <span class="token keyword">int</span> age<span class="token punctuation">;</span>

	<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setName</span><span class="token punctuation">(</span><span class="token class-name">String</span> n<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		name <span class="token operator">=</span> name<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
	<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setAge</span><span class="token punctuation">(</span><span class="token keyword">int</span> a<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		age <span class="token operator">=</span> age<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
	<span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">return</span> name<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
	<span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">getAge</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">return</span> age<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br></div></div><p>不就是设置名字和年龄吗，如果此时把参数名分别改为name和age。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Student</span> <span class="token punctuation">{</span>
	<span class="token keyword">private</span> <span class="token class-name">String</span> name<span class="token punctuation">;</span>
	<span class="token keyword">private</span> <span class="token keyword">int</span> age<span class="token punctuation">;</span>

	<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setName</span><span class="token punctuation">(</span><span class="token class-name">String</span> name<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		name <span class="token operator">=</span> name<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
	<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setAge</span><span class="token punctuation">(</span><span class="token keyword">int</span> age<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		age <span class="token operator">=</span> age<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
	<span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">return</span> name<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
	<span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">getAge</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">return</span> age<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br></div></div><p>此时会发现参数根本就设置不进去，name和age打印出来都是各自的初始值，运行测试类的结果如下：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>null,0
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>先回忆方法的参数属于局部变量这个结论，导致参数设置不进去的原因是：</p> <p>局部变量和成员变量同名，此时在方法中调用变量时根据就近原则，优先使用局部变量，示意图如下。</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/EtT9q2DrnHMWyJk.png" alt=""></p> <p>​		可以看出setName方法中两次使用的name，都是直接寻找距离自己最近的形参name，就相当于把参数name的值设置给参数name，根本就没有把参数值设置给成员变量。</p> <blockquote><p>提示：</p> <p>当在一个作用域访问变量时，首先在当前作用域查找该变量，
如果能找到，不继续查找。
如果在当前作用域找不到该变量，尝试去上一层作用域查找，如果找到，停止查找，如果找不到，继续上一层查找，依次类推。这整个过程形成一个查找链，这个称为作用域链。</p></blockquote> <p>​		该问题，更专业的叫法是<strong>局部变量和成员变量存在二义性</strong>，也就是变量名有歧义。为了解决该问题——有请this关键字。</p> <p>​		使用 <strong>this.变量名</strong> 的语法，此时访问的就是成员变量，this的其他操作，后面再讲。</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/zAM2aLDNV9FxTcn.png" alt=""></strong></p> <p>具体代码如下：</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Student</span> <span class="token punctuation">{</span>
	<span class="token keyword">private</span> <span class="token class-name">String</span> name<span class="token punctuation">;</span>
	<span class="token keyword">private</span> <span class="token keyword">int</span> age<span class="token punctuation">;</span>

	<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setName</span><span class="token punctuation">(</span><span class="token class-name">String</span> name<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
	<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setAge</span><span class="token punctuation">(</span><span class="token keyword">int</span> age<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">this</span><span class="token punctuation">.</span>age <span class="token operator">=</span> age<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
	<span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">return</span> name<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
	<span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">getAge</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">return</span> age<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br></div></div><h4 id="_2-7-2-使用构造器还是setter方法-了解"><a href="#_2-7-2-使用构造器还是setter方法-了解" class="header-anchor">#</a> 2.7.2 使用构造器还是setter方法（了解）</h4> <p>构造器和setter方法都可以给对象设置数据：</p> <ul><li>构造器，在创建对象的时候设置初始数据，只能初始化一次。</li> <li>setter方法，创建对象后再设置初始数据，可以设置多次。</li></ul> <h2 id="三、封装思想"><a href="#三、封装思想" class="header-anchor">#</a> 三、封装思想</h2> <h3 id="_3-1-封装思想概述-理解"><a href="#_3-1-封装思想概述-理解" class="header-anchor">#</a> 3.1 封装思想概述 （理解）</h3> <p>面向对象封装思想要求，使用类去设计对象时
(1) 将需要处理的<strong>数据</strong>
(2) 以及处理这些数据的<strong>方法</strong>,都设计到对象中</p> <p>为了更好地理解它们，我们先来一个需求（先不按封装思想的要求）：</p> <table><thead><tr><th>学号</th> <th>姓名</th> <th>年龄</th> <th>Java成绩</th> <th>Sql成绩</th></tr></thead> <tbody><tr><td>1</td> <td>will</td> <td>17</td> <td>90</td> <td>89</td></tr> <tr><td>2</td> <td>alex</td> <td>18</td> <td>80</td> <td>80</td></tr></tbody></table> <ul><li>计算出每一名学生的总成绩</li> <li>打印输出学生的所有信息</li></ul> <p>没有使用封装：</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// 第一个学生信息</span>
    <span class="token keyword">int</span> id1 <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token class-name">String</span> name1 <span class="token operator">=</span> <span class="token string">&quot;will&quot;</span><span class="token punctuation">;</span>
    <span class="token keyword">int</span> age1 <span class="token operator">=</span> <span class="token number">17</span><span class="token punctuation">;</span>
    <span class="token keyword">float</span> javaScore1 <span class="token operator">=</span> <span class="token number">90</span><span class="token punctuation">;</span>
    <span class="token keyword">float</span> sqlScore1 <span class="token operator">=</span> <span class="token number">89</span><span class="token punctuation">;</span>

    <span class="token comment">// 第二个学生信息</span>
    <span class="token keyword">int</span> id2 <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token class-name">String</span> name2 <span class="token operator">=</span> <span class="token string">&quot;alex&quot;</span><span class="token punctuation">;</span>
    <span class="token keyword">int</span> age2 <span class="token operator">=</span> <span class="token number">20</span><span class="token punctuation">;</span>
    <span class="token keyword">float</span> javaScore2 <span class="token operator">=</span> <span class="token number">80</span><span class="token punctuation">;</span>
    <span class="token keyword">float</span> sqlScore2 <span class="token operator">=</span> <span class="token number">80</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><p>使用封装：把之前声明的变量放入到类中作为成员变量</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Student</span> <span class="token punctuation">{</span>
    <span class="token keyword">int</span> id<span class="token punctuation">;</span>
    <span class="token class-name">String</span> name<span class="token punctuation">;</span>
    <span class="token keyword">int</span> age<span class="token punctuation">;</span>
    <span class="token keyword">float</span> javaScore<span class="token punctuation">;</span>
    <span class="token keyword">float</span> sqlScore<span class="token punctuation">;</span>
	
    <span class="token comment">// 为了便于给变量赋值，类提供一个全参的构造方法</span>
    <span class="token keyword">public</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token keyword">int</span> aId<span class="token punctuation">,</span> <span class="token class-name">String</span> aName<span class="token punctuation">,</span> <span class="token keyword">int</span> aAge<span class="token punctuation">,</span> <span class="token keyword">float</span> aJavaScore<span class="token punctuation">,</span> <span class="token keyword">float</span> aSqlScore<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        id <span class="token operator">=</span> aId<span class="token punctuation">;</span>
        name <span class="token operator">=</span> aName<span class="token punctuation">;</span>
        age <span class="token operator">=</span> aAge<span class="token punctuation">;</span>
        javaScore <span class="token operator">=</span> aJavaScore<span class="token punctuation">;</span>
        sqlScore <span class="token operator">=</span> aSqlScore<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><p>有了类，就可以创建对象，后续只需要管理和维护这两个对象就可以了</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">EncapsulateDemo</span> <span class="token punctuation">{</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 第一个学生信息</span>
        <span class="token class-name">Student</span> stu1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token string">&quot;will&quot;</span><span class="token punctuation">,</span><span class="token number">17</span><span class="token punctuation">,</span><span class="token number">90</span><span class="token punctuation">,</span><span class="token number">89</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 第二个学生信息</span>
        <span class="token class-name">Student</span> stu2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token string">&quot;alex&quot;</span><span class="token punctuation">,</span><span class="token number">20</span><span class="token punctuation">,</span><span class="token number">80</span><span class="token punctuation">,</span><span class="token number">80</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>刚才我们只是把要处理的数据封装好了，回归到需求，还要计算总成绩和打印输出学生信息。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Student</span> <span class="token punctuation">{</span>
    <span class="token keyword">int</span> id<span class="token punctuation">;</span>
    <span class="token class-name">String</span> name<span class="token punctuation">;</span>
    <span class="token keyword">int</span> age<span class="token punctuation">;</span>
    <span class="token keyword">float</span> javaScore<span class="token punctuation">;</span>
    <span class="token keyword">float</span> sqlScore<span class="token punctuation">;</span>

    <span class="token comment">// 省略构造方法</span>

    <span class="token keyword">public</span> <span class="token keyword">float</span> <span class="token function">getTotalScore</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> javaScore <span class="token operator">+</span> sqlScore<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">printInfo</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;学号:&quot;</span> <span class="token operator">+</span> id<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;姓名:&quot;</span> <span class="token operator">+</span> name<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;年龄:&quot;</span> <span class="token operator">+</span> age<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;java成绩:&quot;</span> <span class="token operator">+</span> javaScore<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;sql成绩:&quot;</span> <span class="token operator">+</span> sqlScore<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div><p>调用对象的方法来完成需求</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">EncapsulateDemo</span> <span class="token punctuation">{</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 第一个学生信息</span>
        <span class="token class-name">Student</span> stu1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token string">&quot;will&quot;</span><span class="token punctuation">,</span><span class="token number">17</span><span class="token punctuation">,</span><span class="token number">90</span><span class="token punctuation">,</span><span class="token number">89</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">float</span> totalScore <span class="token operator">=</span> stu1<span class="token punctuation">.</span><span class="token function">getTotalScore</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        stu1<span class="token punctuation">.</span><span class="token function">printInfo</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        
        <span class="token comment">// 第二个学生信息</span>
        <span class="token class-name">Student</span> stu2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token string">&quot;alex&quot;</span><span class="token punctuation">,</span><span class="token number">20</span><span class="token punctuation">,</span><span class="token number">80</span><span class="token punctuation">,</span><span class="token number">80</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// ?</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><p><strong>封装思想的好处：</strong></p> <ul><li>更好的管理和维护数据</li> <li>使用者无需关心内部实现, 只要知道如何使用即可</li></ul> <p>封装 ( encapsulation ) 是面向对象三大特征之一，其含义有两个（掌握思想）：</p> <ul><li><p><strong>把成员变量(用于存数据)和方法(功能) 放在一个类中。</strong></p></li> <li><p>**信息隐藏。**尽可能隐藏对象的数据和功能的实现细节，让使用者更方便的使用即可</p></li></ul> <p>以上封装还不够合理，接下来我们看看封装后数据安全问题：</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Student</span><span class="token punctuation">{</span>
	<span class="token class-name">String</span> name<span class="token punctuation">;</span>
	<span class="token keyword">int</span> age<span class="token punctuation">;</span>

	<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">sayHi</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;我是&quot;</span> <span class="token operator">+</span> name <span class="token operator">+</span> <span class="token string">&quot;,今年&quot;</span> <span class="token operator">+</span> age <span class="token operator">+</span> <span class="token string">&quot;岁&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>测试类：</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">StudentDemo</span> <span class="token punctuation">{</span>
	<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token class-name">Student</span> stu <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		stu<span class="token punctuation">.</span>name <span class="token operator">=</span> <span class="token string">&quot;小明&quot;</span><span class="token punctuation">;</span>
		stu<span class="token punctuation">.</span>age <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">12</span><span class="token punctuation">;</span>
		stu<span class="token punctuation">.</span><span class="token function">sayHi</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>	<span class="token comment">// 我是小明,今年-12岁</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>​		此时从代码语法上来看，是没有任何问题的，但是从逻辑上来分析人的年龄怎么能是负数呢？造成该问题的根本原因就是：可以随意访问对象中的成员变量。</p> <p>​		那么问题来了，怎么才能限制不能随意访问成员变量呢？</p> <p>​		此时，就该访问修饰符登场了！</p> <h3 id="_3-2-访问修饰符-必须记住"><a href="#_3-2-访问修饰符-必须记住" class="header-anchor">#</a> 3.2 访问修饰符（必须记住）</h3> <p>​		车库有一个车位，旁边写着”公共车位”，那么该车位就是公共的，谁都可以访问它。如果我在车位旁边写上“私家车位”，那么该车位就只能是我自己来访问。外界（除我之外）都访问不了，像“公共”、“私有”这种限制外界访问的标记符号，就称之为<strong>访问修饰符</strong>。</p> <p>​		访问修饰符，决定了有没有权限访问某个成员(资源)。</p> <p>​		封装其实就是要让有些类看不到另外一些类中定义的成员变量和方法。Java提供了不同的访问权限修饰符来限定类中的成员让谁可以访问到。</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/FyquPVBS5o9QEHw.png" alt=""></strong></p> <ul><li><strong>private：表示当前类私有的，类访问权限，只能在本类中操作，离开本类之后就不能直接访问</strong></li> <li>不写（默认）：继承再讲</li> <li>protected：继承再讲</li> <li><strong>public：表示公共的，可以在当前项目中任何地方访问</strong></li></ul> <p>暂时记住：把所有的成员变量使用private修饰，所有方法使用public修饰。</p> <h3 id="_3-3-getter方法和setter方法"><a href="#_3-3-getter方法和setter方法" class="header-anchor">#</a> 3.3 getter方法和setter方法</h3> <p>使用private修饰了Student类中的字段，此时在测试类中访问报错。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Student</span> <span class="token punctuation">{</span>
	<span class="token keyword">private</span> <span class="token class-name">String</span> name<span class="token punctuation">;</span>
	<span class="token keyword">private</span> <span class="token keyword">int</span> age<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>测试类：</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">StudentDemo</span> <span class="token punctuation">{</span>
	<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token class-name">Student</span> stu <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		stu<span class="token punctuation">.</span>name <span class="token operator">=</span> <span class="token string">&quot;小明&quot;</span><span class="token punctuation">;</span>	<span class="token comment">//	此行报错，访问权限不足</span>
		stu<span class="token punctuation">.</span>age <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">12</span><span class="token punctuation">;</span>		 <span class="token comment">//	此行报错，访问权限不足</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>此时使用private修饰成员变量后，在测试类中不能再操作这些字段了，怎么办？使用getter方法和setter方法</p> <ul><li><strong>getter: getter方法，也叫获取器，用于获取成员变量的值</strong></li> <li><strong>setter: setter方法，也叫设置器，用于设置成员变量的值</strong></li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Student</span> <span class="token punctuation">{</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> name<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token keyword">int</span> age<span class="token punctuation">;</span>

    <span class="token comment">// getter方法用于获取name的值 命名：get+成员变量首字母大写</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> name<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">// setter方法用于设置name的值 命名: set+成员变量首字母大写</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setName</span><span class="token punctuation">(</span><span class="token class-name">String</span> aName<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        name <span class="token operator">=</span> aName<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">getAge</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> age<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setAge</span><span class="token punctuation">(</span><span class="token keyword">int</span> aAge<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span> aAge <span class="token operator">&lt;</span> <span class="token number">0</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;数据有误&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
            age <span class="token operator">=</span> aAge<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token class-name">String</span> aName<span class="token punctuation">,</span><span class="token keyword">int</span> aAge<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        name <span class="token operator">=</span> aName<span class="token punctuation">;</span>
        age <span class="token operator">=</span> aAge<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>


    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">sayHi</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;name = &quot;</span> <span class="token operator">+</span> name<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;age = &quot;</span> <span class="token operator">+</span> age<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br></div></div><h3 id="_3-4-javabean规范-重点掌握"><a href="#_3-4-javabean规范-重点掌握" class="header-anchor">#</a> 3.4 JavaBean规范（重点掌握）</h3> <p>JavaBean是一种某些符合条件的特殊类，但是必须遵循一定的规范：</p> <ul><li><strong>类必须使用public修饰</strong></li> <li><strong>成员变量使用private修饰(成员变量私有化)</strong></li> <li><strong>每个成员变量提供一对getter和setter方法</strong></li> <li><strong>必须保证有公共无参数构造器</strong>。即使手动提供了带参数的构造器，也得手动提供无参数构造器</li></ul> <p>需求：针对名为name的成员变量来举例</p> <p><strong>getter方法：仅仅用于返回某一个成员变量的值</strong>。getter方法约定命名方式：<strong>get+成员变量首字母大写</strong>。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">return</span> name<span class="token punctuation">;</span>	<span class="token comment">// 返回name字段存储的值</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>注意：如果操作的字段是boolean类型的，此时是is方法，把 getName 变成 isName。</p> <p><strong>setter方法：仅仅用来给某一个成员变量设置值</strong>，setter方法约定命名方式：<strong>set+成员变量首字母大写</strong>。实际开发过程中，可以融入校验逻辑。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setName</span><span class="token punctuation">(</span><span class="token class-name">String</span> aName<span class="token punctuation">)</span><span class="token punctuation">{</span>
    name <span class="token operator">=</span> aName<span class="token punctuation">;</span>	<span class="token comment">// 把传过来的参数aName的值,存储到name字段中</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>注意：每一个字段都得使用private修饰，并提供一对getter/setter方法。</p> <p>IDEA工具可以自动生成标准的getter/setter，前期必须手写。</p> <p>代码如下：</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Student</span> <span class="token punctuation">{</span>
	<span class="token keyword">private</span> <span class="token class-name">String</span> name<span class="token punctuation">;</span>
	<span class="token keyword">private</span> <span class="token keyword">int</span> age<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token keyword">boolean</span> graduate    <span class="token comment">// 是否毕业</span>
	
	<span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">return</span> name<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
	<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setName</span><span class="token punctuation">(</span><span class="token class-name">String</span> aName<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		name <span class="token operator">=</span> aName<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
    
	<span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">getAge</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">return</span> age<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
	<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setAge</span><span class="token punctuation">(</span><span class="token keyword">int</span> aAge<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>aAge <span class="token operator">&lt;</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
			<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;非法的年龄&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            age <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
		<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
            age <span class="token operator">=</span> aAge<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
	<span class="token punctuation">}</span>
    
    <span class="token comment">// 如果字段时boolean类型，getter方法可以是isGraduate或getGraduate，从语义上讲，更推荐isGraduate</span>
    <span class="token keyword">public</span> <span class="token keyword">boolean</span> <span class="token function">isGraduate</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> graduate<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setGraduate</span><span class="token punctuation">(</span><span class="token keyword">boolean</span> aGraduate<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        graduate <span class="token operator">=</span> aGraduate<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    
    <span class="token comment">// 其他代码</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br></div></div><p>测试类：</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">StudentDemo</span> <span class="token punctuation">{</span>
	<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token class-name">Student</span> stu <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token comment">// 调用setter方法设置字段数据</span>
		stu<span class="token punctuation">.</span><span class="token function">setName</span><span class="token punctuation">(</span><span class="token string">&quot;小明&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		stu<span class="token punctuation">.</span><span class="token function">setAge</span><span class="token punctuation">(</span><span class="token number">12</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        
		<span class="token comment">// 调用getter方法获取字段数据</span>
		<span class="token class-name">String</span> name <span class="token operator">=</span> stu<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">int</span> age <span class="token operator">=</span> stu<span class="token punctuation">.</span><span class="token function">getAge</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>name <span class="token operator">+</span> <span class="token string">&quot;,&quot;</span> <span class="token operator">+</span> age<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><h3 id="_3-5-包管理"><a href="#_3-5-包管理" class="header-anchor">#</a> 3.5 包管理</h3> <h4 id="_3-5-1-包定义-package-会用"><a href="#_3-5-1-包定义-package-会用" class="header-anchor">#</a> 3.5.1 包定义-package（会用）</h4> <p>包的作用：用于管理和组织类，类似于磁盘的文件夹一样( <strong>包可以被看成目录</strong> )，定义语法为：</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code>语法格式<span class="token operator">:</span> 
<span class="token keyword">package</span> 包名<span class="token punctuation">.</span>子包名<span class="token punctuation">.</span>子子包<span class="token punctuation">;</span> 

例如<span class="token operator">:</span> 
<span class="token keyword">package</span> <span class="token namespace">cn<span class="token punctuation">.</span>wolfcode</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>注意：</p> <ul><li><p>必须把该语句作为Java文件中第一行代码（所有代码之前）</p></li> <li><p>包名建议都使用全<strong>小写字母</strong>，命名<strong>符合标识符规则</strong></p></li> <li><p>父包和子包之间使用点符号（.）分割</p></li> <li><p>因为Java的安全机制，自定义包名<strong>不允许使用</strong>java单词作为包名。</p></li></ul> <p>企业中起名规范（后续项目期）：</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">package</span> 企业域名倒写<span class="token punctuation">.</span>模块名<span class="token punctuation">.</span>组件名<span class="token punctuation">;</span> 
如<span class="token operator">:</span><span class="token keyword">package</span> <span class="token namespace">cn<span class="token punctuation">.</span>wolfcode<span class="token punctuation">.</span>array</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>分包效果如下图：</p> <p><em><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/DZJISGTonvQuEmA.png" alt=""></em></p> <p>选中一个包后，创建出来的类 ( 就位于该包中 )，代码如下：</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">package</span> <span class="token namespace">cn<span class="token punctuation">.</span>wolfcode<span class="token punctuation">.</span>array</span><span class="token punctuation">;</span>	<span class="token comment">//当前类所在的包 </span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ArrayDemo</span> <span class="token punctuation">{</span> 
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">int</span> <span class="token function">getSum</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr1<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">int</span> sum <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> ele<span class="token operator">:</span> arr<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            sum <span class="token operator">+=</span> ele<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">return</span> sum<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>此时，类的名称：</p> <p>简单名称：ArrayDemo</p> <p>全限定名：包名.简单名称，即 cn.wolfcode.array.ArrayDemo</p> <h4 id="_3-5-2-导包-import-掌握"><a href="#_3-5-2-导包-import-掌握" class="header-anchor">#</a> 3.5.2 导包-import（掌握）</h4> <p>在MethodDemo代码中使用ArrayDemo类中的getSum方法对数组元素进行求和</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">package</span> <span class="token namespace">cn<span class="token punctuation">.</span>wolfcode<span class="token punctuation">.</span>method</span><span class="token punctuation">;</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MethodDemo</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
        <span class="token keyword">int</span> r <span class="token operator">=</span> <span class="token class-name"><span class="token namespace">cn<span class="token punctuation">.</span>wolfcode<span class="token punctuation">.</span>array<span class="token punctuation">.</span></span>ArrayDemo</span><span class="token punctuation">.</span><span class="token function">getSum</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>r<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>注意：</p> <p>ArrayDemo类在cn.wolfcode.array包中，不在cn.wolfcode.method包中, 此时，必须使用全限定名称——cn.wolfcode.array.ArrayDemo 去调用方法。</p> <p>每次使用getSum方法，都必须使用ArrayDemo 类的全限定名来调用方法，确实很累，所以此时就该import关键字出场了。</p> <p><strong>语法</strong>:</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">import</span> 类的全限定名<span class="token punctuation">;</span>   <span class="token comment">// 表示只导入一个类. </span>
<span class="token keyword">import</span> 包名<span class="token punctuation">.</span>*<span class="token punctuation">;</span> 		<span class="token comment">// 表示会引入该包下的所有在当前文件中使用到的类</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>小结：对package定义包和import导入某个类，会用就可以了，不深究。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">package</span> <span class="token namespace">cn<span class="token punctuation">.</span>wolfcode<span class="token punctuation">.</span>method</span><span class="token punctuation">;</span>

<span class="token keyword">import</span> <span class="token import"><span class="token namespace">cn<span class="token punctuation">.</span>wolfcode<span class="token punctuation">.</span>array<span class="token punctuation">.</span></span><span class="token class-name">ArrayDemo</span></span><span class="token punctuation">;</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MethodDemo</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
        <span class="token keyword">int</span> r <span class="token operator">=</span> <span class="token class-name">ArrayDemo</span><span class="token punctuation">.</span><span class="token function">getSum</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>r<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><h2 id="四、小结与作业"><a href="#四、小结与作业" class="header-anchor">#</a> 四、小结与作业</h2> <h3 id="_4-1-小结"><a href="#_4-1-小结" class="header-anchor">#</a> 4.1 小结</h3> <ul><li><p>了解软件开发方式--面向过程 + 面向对象</p></li> <li><p>成员变量和局部变量-位置 + 作用域 + 初始值 + 生命周期</p></li> <li><p>类--类的概念 + 类的定义 + 类的使用</p></li> <li><p>对象--对象的概念 + 对象创建 + 对象基本操作（操作字段和调用方法）+  内存图理解对象实例化，操作字段，调用方法的过程</p></li> <li><p>构造器--构造器的作用和定义 + 默认构造器 + 有参构造器</p></li> <li><p>封装思想- 封装思想的2层含义+掌握private和public修饰符</p></li> <li><p>JavaBean规范</p></li> <li><p>包的定义与导入</p></li></ul> <h3 id="_4-2-作业"><a href="#_4-2-作业" class="header-anchor">#</a> 4.2 作业</h3> <h4 id="_4-2-1-题目1"><a href="#_4-2-1-题目1" class="header-anchor">#</a> 4.2.1 题目1</h4> <p>定义手机类，手机有品牌(brand),价格(price)和颜色(color)三个属性，有打电话call()和sendMessage()两个功能。</p> <p>请定义出手机类，类中要有空参、有参构造方法，set/get方法。</p> <p>定义测试类，在主方法中使用空参构造创建对象，使用set方法赋值。</p> <p>调用对象的两个功能，打印效果如下：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>正在使用价格为3998元黑色的小米手机打电话....
正在使用价格为3998元黑色的小米手机发短信....
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>训练提示</p> <ol><li>类中的属性就是成员变量，类中的行为功能就是成员方法。</li> <li>成员变量要被private修饰。</li></ol> <p>解题方案</p> <p>操作步骤</p> <ol><li>定义手机类，手机类中定义String类型的品牌，int类型的价格，String类型的颜色，三个成员变量都用privice修饰。</li> <li>提供空参构造方法和有参构造方法。</li> <li>提供set/get方法。</li> <li>编写打电话的成员方法，方法中对成员变量进行使用。</li> <li>编写发短信的成员方法，方法中对成员变量进行使用。</li> <li>在测试类中创建手机对象，使用set方法赋值，分别调用各个方法。</li></ol> <p>参考答案</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code>手机类：
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Phone</span> <span class="token punctuation">{</span>
    <span class="token comment">//定义品牌</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> brand<span class="token punctuation">;</span>
    <span class="token comment">//定义价格</span>
    <span class="token keyword">private</span> <span class="token keyword">int</span> price<span class="token punctuation">;</span>
    <span class="token comment">//定义颜色</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> color<span class="token punctuation">;</span>

    <span class="token comment">//提供空参构造</span>
    <span class="token keyword">public</span> <span class="token class-name">Phone</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token punctuation">}</span>
    <span class="token comment">//提供有参构造</span>
    <span class="token keyword">public</span> <span class="token class-name">Phone</span><span class="token punctuation">(</span><span class="token class-name">String</span> brand<span class="token punctuation">,</span> <span class="token keyword">int</span> price<span class="token punctuation">,</span> <span class="token class-name">String</span> color<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>brand <span class="token operator">=</span> brand<span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>price <span class="token operator">=</span> price<span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>color <span class="token operator">=</span> color<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">//提供set/get方法</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">getBrand</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> brand<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setBrand</span><span class="token punctuation">(</span><span class="token class-name">String</span> brand<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>brand <span class="token operator">=</span> brand<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token keyword">double</span> <span class="token function">getPrice</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> price<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setPrice</span><span class="token punctuation">(</span><span class="token keyword">int</span> price<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>price <span class="token operator">=</span> price<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">getColor</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> color<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setColor</span><span class="token punctuation">(</span><span class="token class-name">String</span> color<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>color <span class="token operator">=</span> color<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">//定义打电话方法</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">call</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;正在使用价格为&quot;</span><span class="token operator">+</span>price<span class="token operator">+</span><span class="token string">&quot;元&quot;</span><span class="token operator">+</span>color<span class="token operator">+</span><span class="token string">&quot;的&quot;</span><span class="token operator">+</span>brand<span class="token operator">+</span><span class="token string">&quot;手机打电话....&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">//定义发短信方法</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">sendMessage</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;正在使用价格为&quot;</span><span class="token operator">+</span>price<span class="token operator">+</span><span class="token string">&quot;元&quot;</span><span class="token operator">+</span>color<span class="token operator">+</span><span class="token string">&quot;的&quot;</span><span class="token operator">+</span>brand<span class="token operator">+</span><span class="token string">&quot;手机发短信....&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

测试类：
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Demo1</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">//创建手机对象</span>
        <span class="token class-name">Phone</span> p <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Phone</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//调用set方法赋值</span>
        p<span class="token punctuation">.</span><span class="token function">setBrand</span><span class="token punctuation">(</span><span class="token string">&quot;小米&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        p<span class="token punctuation">.</span><span class="token function">setPrice</span><span class="token punctuation">(</span><span class="token number">3998</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        p<span class="token punctuation">.</span><span class="token function">setColor</span><span class="token punctuation">(</span><span class="token string">&quot;黑色&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//调用打电话功能</span>
        p<span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//调用发短信功能</span>
        p<span class="token punctuation">.</span><span class="token function">sendMessage</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br><span class="line-number">61</span><br><span class="line-number">62</span><br></div></div><h4 id="_4-2-2-题目2"><a href="#_4-2-2-题目2" class="header-anchor">#</a> 4.2.2 题目2</h4> <p>定义一个女朋友类。女朋友的属性包含：姓名，身高，体重。行为包含：洗衣服wash()，做饭cook()。另外定义一个用于展示三个属性值的show()方法。请在测试类中通过有参构造方法创建对象并赋值，然后分别调用展示方法、洗衣服方法和做饭方法。打印效果如下：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>我女朋友叫凤姐,身高155.0厘米,体重130.0斤
女朋友帮我洗衣服
女朋友给我做饭
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>训练提示</p> <ol><li>类中的属性就是成员变量，类中的行为功能就是成员方法。</li> <li>成员变量要被private修饰。</li> <li>展示方法的作用就是打印姓名、身高、体重三个成员变量的值。</li></ol> <p>解题方案</p> <p>操作步骤</p> <ol><li>定义女朋友类，定义String类型姓名，double类型身高和double类型体重三个成员变量，三个成员变量都用privice修饰。</li> <li>提供空参构造方法和有参构造方法。</li> <li>提供set/get方法。</li> <li>编写展示方法show()，方法打印三个成员变量的值。</li> <li>编写洗衣服wash()方法，输出洗衣服的语句。</li> <li>编写做饭cook()方法，输出做饭的语句。</li> <li>在测试类中使用有参构造创建女友对象，分别调用各个方法。</li></ol> <p>参考答案</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code>女朋友类：
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Girlfriend</span> <span class="token punctuation">{</span>
    <span class="token comment">//姓名</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> name<span class="token punctuation">;</span>
    <span class="token comment">//身高</span>
    <span class="token keyword">private</span> <span class="token keyword">double</span> height<span class="token punctuation">;</span>
    <span class="token comment">//体重</span>
    <span class="token keyword">private</span> <span class="token keyword">double</span> weight<span class="token punctuation">;</span>
    <span class="token comment">//空参构造</span>
    <span class="token keyword">public</span> <span class="token class-name">Girlfriend</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token punctuation">}</span>
    <span class="token comment">//有参构造</span>
    <span class="token keyword">public</span> <span class="token class-name">Girlfriend</span><span class="token punctuation">(</span><span class="token class-name">String</span> name<span class="token punctuation">,</span> <span class="token keyword">double</span> height<span class="token punctuation">,</span> <span class="token keyword">double</span> weight<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>height <span class="token operator">=</span> height<span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>weight <span class="token operator">=</span> weight<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">//set/get方法</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> name<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setName</span><span class="token punctuation">(</span><span class="token class-name">String</span> name<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token keyword">double</span> <span class="token function">getHeight</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> height<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setHeight</span><span class="token punctuation">(</span><span class="token keyword">double</span> height<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>height <span class="token operator">=</span> height<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token keyword">double</span> <span class="token function">getWeight</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> weight<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setWeight</span><span class="token punctuation">(</span><span class="token keyword">double</span> weight<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>weight <span class="token operator">=</span> weight<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">//展示方法</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">show</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;我女朋友叫&quot;</span> <span class="token operator">+</span>name<span class="token operator">+</span><span class="token string">&quot;,身高&quot;</span> <span class="token operator">+</span>height<span class="token operator">+</span><span class="token string">&quot;厘米,体重&quot;</span><span class="token operator">+</span>weight<span class="token operator">+</span><span class="token string">&quot;斤&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">//洗衣服方法</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">wash</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;女朋友帮我洗衣服&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">//做饭方法</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">cook</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;女朋友给我做饭&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
测试类：
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Demo2</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">//创建女朋友对象</span>
        <span class="token class-name">Girlfriend</span> gf <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Girlfriend</span><span class="token punctuation">(</span><span class="token string">&quot;凤姐&quot;</span><span class="token punctuation">,</span><span class="token number">155</span><span class="token punctuation">,</span><span class="token number">130</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//调用展示方法</span>
        gf<span class="token punctuation">.</span><span class="token function">show</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//调用洗衣服方法</span>
        gf<span class="token punctuation">.</span><span class="token function">wash</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//调用做饭方法</span>
        gf<span class="token punctuation">.</span><span class="token function">cook</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br><span class="line-number">61</span><br><span class="line-number">62</span><br></div></div><h4 id="_4-2-3-题目3"><a href="#_4-2-3-题目3" class="header-anchor">#</a> 4.2.3 题目3</h4> <p>定义项目经理类Manager。属性：姓名name，工号id，工资salary，奖金bonus。行为：工作work()
定义程序员类Coder。属性：姓名name，工号id，工资salary。行为：工作work()</p> <p>要求：</p> <div class="language- extra-class"><pre><code>1.按照以上要求定义Manager类和Coder类,属性要私有,生成空参、有参构造，set和get方法
2.定义测试类,在main方法中创建该类的对象并给属性赋值(set方法或有参构造方法)
3.调用成员方法,打印格式如下:		
</code></pre></div><div class="language- line-numbers-mode"><pre class="language-text"><code>工号为123基本工资为15000奖金为6000的项目经理正在努力的做着管理工作,分配任务,检查员工提交上来的代码.....
工号为135基本工资为10000的程序员正在努力的写着代码......
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>训练提示</p> <ol><li>类中的属性就是成员变量，类中的行为功能就是成员方法。</li> <li>成员变量要被private修饰。</li> <li>在工作work()方法中调用成员变量，输出成员变量的值。</li></ol> <p>解题方案</p> <p>操作步骤</p> <ol><li>定义项目经理类，定义成员变量，构造方法，set和get方法，work方法，方法中根据打印格式输出id,salary,bonus的值。</li> <li>定义程序猿类，定义成员变量，构造方法，set和get方法，work方法，方法中根据打印格式输出id和salary的值。</li> <li>在测试类中使用有参构造创建项目经理对象并赋值，调用工作方法打印结果。</li> <li>在测试类中使用有参构造创建程序员对象并赋值，调用工作方法打印结果。</li></ol> <p>参考答案</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code>经理类<span class="token operator">:</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Manager</span> <span class="token punctuation">{</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> name<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token keyword">int</span> id<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token keyword">int</span> salary<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token keyword">int</span> bonus<span class="token punctuation">;</span>
    <span class="token keyword">public</span> <span class="token class-name">Manager</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token class-name">Manager</span><span class="token punctuation">(</span><span class="token class-name">String</span> name<span class="token punctuation">,</span> <span class="token keyword">int</span> id<span class="token punctuation">,</span> <span class="token keyword">int</span> salary<span class="token punctuation">,</span> <span class="token keyword">int</span> bonus<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>id <span class="token operator">=</span> id<span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>salary <span class="token operator">=</span> salary<span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>bonus <span class="token operator">=</span> bonus<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> name<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setName</span><span class="token punctuation">(</span><span class="token class-name">String</span> name<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">getId</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> id<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setId</span><span class="token punctuation">(</span><span class="token keyword">int</span> id<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>id <span class="token operator">=</span> id<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">getSalary</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> salary<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setSalary</span><span class="token punctuation">(</span><span class="token keyword">int</span> salary<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>salary <span class="token operator">=</span> salary<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">getBonus</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> bonus<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setBonus</span><span class="token punctuation">(</span><span class="token keyword">int</span> bonus<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>bonus <span class="token operator">=</span> bonus<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">work</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;工号为&quot;</span><span class="token operator">+</span>id<span class="token operator">+</span><span class="token string">&quot;基本工资为&quot;</span><span class="token operator">+</span>salary<span class="token operator">+</span><span class="token string">&quot;奖金为&quot;</span><span class="token operator">+</span>bonus<span class="token operator">+</span><span class="token string">&quot;的项目经理正在努力的做着管理工作,分配任务,检查员工提交上来的代码.....&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
程序员类：
<span class="token keyword">package</span> <span class="token namespace">com<span class="token punctuation">.</span>day07</span><span class="token punctuation">;</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Coder</span> <span class="token punctuation">{</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> name<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token keyword">int</span> id<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token keyword">int</span> salary<span class="token punctuation">;</span>
    <span class="token keyword">public</span> <span class="token class-name">Coder</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token class-name">Coder</span><span class="token punctuation">(</span><span class="token class-name">String</span> name<span class="token punctuation">,</span> <span class="token keyword">int</span> id<span class="token punctuation">,</span> <span class="token keyword">int</span> salary<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>id <span class="token operator">=</span> id<span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>salary <span class="token operator">=</span> salary<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> name<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setName</span><span class="token punctuation">(</span><span class="token class-name">String</span> name<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">getId</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> id<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setId</span><span class="token punctuation">(</span><span class="token keyword">int</span> id<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>id <span class="token operator">=</span> id<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">getSalary</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> salary<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setSalary</span><span class="token punctuation">(</span><span class="token keyword">int</span> salary<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>salary <span class="token operator">=</span> salary<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">work</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;工号为&quot;</span><span class="token operator">+</span>id<span class="token operator">+</span><span class="token string">&quot;基本工资为&quot;</span><span class="token operator">+</span>salary<span class="token operator">+</span><span class="token string">&quot;的程序员正在努力的写着代码......&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
测试类：
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Demo3</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">//创建经理对象</span>
        <span class="token class-name">Manager</span> m <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Manager</span><span class="token punctuation">(</span><span class="token string">&quot;老王&quot;</span><span class="token punctuation">,</span><span class="token number">123</span><span class="token punctuation">,</span><span class="token number">15000</span><span class="token punctuation">,</span><span class="token number">6000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//调用工作方法</span>
        m<span class="token punctuation">.</span><span class="token function">work</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//创建程序员对象</span>
        <span class="token class-name">Coder</span> c <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Coder</span><span class="token punctuation">(</span><span class="token string">&quot;小王&quot;</span><span class="token punctuation">,</span><span class="token number">135</span><span class="token punctuation">,</span><span class="token number">10000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//调用工作方法</span>
        c<span class="token punctuation">.</span><span class="token function">work</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br><span class="line-number">61</span><br><span class="line-number">62</span><br><span class="line-number">63</span><br><span class="line-number">64</span><br><span class="line-number">65</span><br><span class="line-number">66</span><br><span class="line-number">67</span><br><span class="line-number">68</span><br><span class="line-number">69</span><br><span class="line-number">70</span><br><span class="line-number">71</span><br><span class="line-number">72</span><br><span class="line-number">73</span><br><span class="line-number">74</span><br><span class="line-number">75</span><br><span class="line-number">76</span><br><span class="line-number">77</span><br><span class="line-number">78</span><br><span class="line-number">79</span><br><span class="line-number">80</span><br><span class="line-number">81</span><br><span class="line-number">82</span><br><span class="line-number">83</span><br><span class="line-number">84</span><br><span class="line-number">85</span><br><span class="line-number">86</span><br><span class="line-number">87</span><br><span class="line-number">88</span><br><span class="line-number">89</span><br><span class="line-number">90</span><br><span class="line-number">91</span><br></div></div><h4 id="_4-2-4-题目4"><a href="#_4-2-4-题目4" class="header-anchor">#</a> 4.2.4 题目4</h4> <p>定义猫类Cat。属性:毛的颜色color，品种breed。行为:吃饭eat()，抓老鼠catchMouse()
定义狗类Dog。属性:毛的颜色color，品种breed。行为:吃饭()，看家lookHome()
要求:
1.按照以上要求定义Cat类和Dog类,属性要私有,生成空参、有参构造，set和get方法
2.定义测试类,在main方法中创建该类的对象并给属性赋值(set方法或有参构造方法)
3.调用成员方法,打印格式如下:</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>花色的波斯猫正在吃鱼.....
花色的波斯猫正在逮老鼠....
黑色的藏獒正在啃骨头.....
黑色的藏獒正在看家.....
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>训练提示</p> <ol><li>类中的属性就是成员变量，类中的行为功能就是成员方法。</li> <li>成员变量要被private修饰。</li></ol> <p>解题方案</p> <p>操作步骤</p> <ol><li>定义猫类，定义成员变量，构造方法，set和get方法，吃饭方法eat()，抓老鼠方法catchMouse()，方法中根据题目给出的格式输出成员变量的值。</li> <li>定义狗类，定义成员变量，构造方法，set和get方法，吃饭方法eat()，看家方法lookHome()，方法中根据题目给出的格式输出成员变量的值。</li> <li>在测试类中使用有参构造创建猫类对象，调用eat()方法和catchMouse()方法。</li> <li>在测试类中使用有参构造创建狗类对象，调用eat()方法和lookHome()方法。</li></ol> <p>参考答案</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code>猫类：
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Cat</span> <span class="token punctuation">{</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> color<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> breed<span class="token punctuation">;</span>
    <span class="token keyword">public</span> <span class="token class-name">Cat</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token class-name">Cat</span><span class="token punctuation">(</span><span class="token class-name">String</span> color<span class="token punctuation">,</span> <span class="token class-name">String</span> breed<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>color <span class="token operator">=</span> color<span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>breed <span class="token operator">=</span> breed<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">getColor</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> color<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setColor</span><span class="token punctuation">(</span><span class="token class-name">String</span> color<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>color <span class="token operator">=</span> color<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">getBreed</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> breed<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setBreed</span><span class="token punctuation">(</span><span class="token class-name">String</span> breed<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>breed <span class="token operator">=</span> breed<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">eat</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>color<span class="token operator">+</span><span class="token string">&quot;的&quot;</span><span class="token operator">+</span>breed<span class="token operator">+</span><span class="token string">&quot;正在吃鱼.....&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">catchMouse</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>color <span class="token operator">+</span> <span class="token string">&quot;的&quot;</span><span class="token operator">+</span> breed <span class="token operator">+</span><span class="token string">&quot;正在逮老鼠....&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
狗类：
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Dog</span> <span class="token punctuation">{</span>
	<span class="token keyword">private</span> <span class="token class-name">String</span> color<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> breed<span class="token punctuation">;</span>
    <span class="token keyword">public</span> <span class="token class-name">Dog</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token class-name">Dog</span><span class="token punctuation">(</span><span class="token class-name">String</span> color<span class="token punctuation">,</span> <span class="token class-name">String</span> breed<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>color <span class="token operator">=</span> color<span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>breed <span class="token operator">=</span> breed<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">getColor</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> color<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setColor</span><span class="token punctuation">(</span><span class="token class-name">String</span> color<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>color <span class="token operator">=</span> color<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">getBreed</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> breed<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setBreed</span><span class="token punctuation">(</span><span class="token class-name">String</span> breed<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>breed <span class="token operator">=</span> breed<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">eat</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>color <span class="token operator">+</span> <span class="token string">&quot;的&quot;</span><span class="token operator">+</span> breed <span class="token operator">+</span><span class="token string">&quot;正在啃骨头.....&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">lookhome</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>color <span class="token operator">+</span> <span class="token string">&quot;的&quot;</span><span class="token operator">+</span> breed <span class="token operator">+</span><span class="token string">&quot;正在看家.....&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
测试类：
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Demo4</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">//创建猫对象</span>
        <span class="token class-name">Cat</span> c <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Cat</span><span class="token punctuation">(</span><span class="token string">&quot;花色&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;波斯猫&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//调用猫的方法</span>
        c<span class="token punctuation">.</span><span class="token function">eat</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        c<span class="token punctuation">.</span><span class="token function">catchMouse</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//创建狗对象</span>
        <span class="token class-name">Dog</span> d <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Dog</span><span class="token punctuation">(</span><span class="token string">&quot;黑色&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;藏獒&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        d<span class="token punctuation">.</span><span class="token function">eat</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        d<span class="token punctuation">.</span><span class="token function">lookhome</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br><span class="line-number">61</span><br><span class="line-number">62</span><br><span class="line-number">63</span><br><span class="line-number">64</span><br><span class="line-number">65</span><br><span class="line-number">66</span><br><span class="line-number">67</span><br><span class="line-number">68</span><br><span class="line-number">69</span><br><span class="line-number">70</span><br></div></div></div></div>  <div class="page-edit"><!----> <div class="tags"><a href="/tags/?tag=pojo" title="标签">#pojo</a><a href="/tags/?tag=class" title="标签">#class</a><a href="/tags/?tag=Object" title="标签">#Object</a></div> <!----></div> <div class="page-nav-wapper"><div class="page-nav-centre-wrap"><a href="/pages/0840c0/" class="page-nav-centre page-nav-centre-prev"><div class="tooltip">方法定义与使用</div></a> <a href="/pages/6db17e/" class="page-nav-centre page-nav-centre-next"><div class="tooltip">继承与抽象</div></a></div> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a href="/pages/0840c0/" class="prev">方法定义与使用</a></span> <span class="next"><a href="/pages/6db17e/">继承与抽象</a>→
      </span></p></div></div></div> <div class="article-list"><div class="article-title"><a href="/archives/" class="iconfont icon-bi">最近更新</a></div> <div class="article-wrapper"><dl><dd>01</dd> <dt><a href="/pages/0c07b2/"><div>
            IT杂货铺
            <!----></div></a> <span class="date">03-23</span></dt></dl><dl><dd>02</dd> <dt><a href="/pages/705b35/"><div>
            人事百问
            <!----></div></a> <span class="date">03-22</span></dt></dl><dl><dd>03</dd> <dt><a href="/pages/78eb56/"><div>
            MyBatis-Plus
            <!----></div></a> <span class="date">03-12</span></dt></dl> <dl><dd></dd> <dt><a href="/archives/" class="more">更多文章&gt;</a></dt></dl></div></div></main></div> <div class="footer"><div class="icons"><a href="mailto:langfeiyes@163.com" title="发邮件" target="_blank" class="iconfont icon-youjian"></a><a href="https://github.com/langfeiyes" title="GitHub" target="_blank" class="iconfont icon-github"></a><a href="https://music.163.com/#/playlist?id=755597173" title="听音乐" target="_blank" class="iconfont icon-erji"></a></div> 
  Theme by
  <a href="https://github.com/xugaoyi/vuepress-theme-vdoing" target="_blank" title="本站主题">Vdoing</a> 
    | Copyright © 2024-2024
    <span><a href="https://beian.miit.gov.cn/" target="_blank">粤ICP备18007927号-1</a></span></div> <div class="buttons"><div title="返回顶部" class="button blur go-to-top iconfont icon-fanhuidingbu" style="display:none;"></div> <div title="去评论" class="button blur go-to-comment iconfont icon-pinglun" style="display:none;"></div> <div title="主题模式" class="button blur theme-mode-but iconfont icon-zhuti"><ul class="select-box" style="display:none;"><li class="iconfont icon-zidong">
          跟随系统
        </li><li class="iconfont icon-rijianmoshi">
          浅色模式
        </li><li class="iconfont icon-yejianmoshi">
          深色模式
        </li><li class="iconfont icon-yuedu">
          阅读模式
        </li></ul></div></div> <!----> <!----> <!----></div><div class="global-ui"><div></div></div></div>
    <script src="/assets/js/app.a2a00aa5.js" defer></script><script src="/assets/js/2.256f807a.js" defer></script><script src="/assets/js/18.4536fd5c.js" defer></script>
  </body>
</html>
